<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
    <title>Title</title>
    {{ JSGlue.include() }}
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery
/jquery-1.4.min.js"></script>
</head>
<body>
<p><a href="{{ url_for('add_api') }}" class="btn btn-primary">新增</a></p>
<div class='page'>
    {% macro list_apis(apis) -%}
    <table border="0" cellspacing="0" cellpadding="0" class="table table-striped" id="msg-table">

        <tr>

            <th width="5%">ID</th>
            <th width="10%">名称</th>
            <th width="10%">接口</th>
            <th width="10%">状态</th>
            <th width="10%">开关</th>
            <th >操作</th>

        </tr>


        {% for api in apis %}
        <tr>

            <td >{{api.id}}</td>
            <td >{{api.name}}</td>
            <td>{{api.api}}</td>
            <td >{{api.status}}</td>
            <td >{{ caller(api.id,api.status) }}</td>

        </tr>
        {% endfor %}
    </table>
    {%- endmacro %}
    {% call(id,status) list_apis(apis) %}
    {% if status=='on' %}
    <a href="{{ url_for('switch_status',id = id, status='off') }}">turn off</a>
    {% else %}
    <a href="{{ url_for('switch_status',id = id, status='on') }}">turn on</a>
    {% endif %}
    <td><a href="{{ url_for('edit_api',id=id) }}">编辑</a>
    <a href="{{ url_for('delete_api',id=id) }}">删除</a></td>
    {% endcall %}

    <a href="{{ url_for('add_field_info')}}" class="btn btn-primary">新增</a>
    <table class="table table-striped">
        <tr>

            <th width="5%">ID</th>
            <th width="10%">名称</th>
            <th width="10%">接口</th>
            <th width="10%">状态</th>
            <th width="10%">开关</th>
            <th width="10%">操作</th>
            <th >可选项</th>
        </tr>
        {% for field in fields %}
        <tr>
            <td >{{field.id}}</td>
            <td >{{field.name}}</td>
            <td >{{field.api}}</td>
            <td>{{field.status}}</td>
            {% if field.status=='on' %}
            <td><a href="{{ url_for('switch_status_field',id = field.id, status='off') }}">turn off</a></td>
            {% else %}
            <td><a href="{{ url_for('switch_status_field',id = field.id, status='on') }}">turn on</a></td>
            {% endif %}
            <td><a href="{{ url_for('edit_fieldinfo',id=field.id) }}">编辑</a></td>
<td>
            {% if field.isbool == 1 %}
            {% for key,value in field.setting.items() %}


                    {% if value[1] == 1 %}
                    <input type="checkbox" name="isbool"
                               onclick="check(this,'{{value[0]}}','{{field.name}}','{{field.api}}')" checked >{{ key }}

                    {% else %}
                    <input type="checkbox" name="isbool"
                               onclick="check(this,'{{value[0]}}','{{field.name}}','{{field.api}}')" >{{ key }}

                    {% endif %}




            {% endfor %}
            {% else %}
            {% for key,value in field.setting.items() %}

                    {% if value["radio_ischecked"] == 1 %}
                    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked
                               onclick="check_radio(this,'{{value}}}','{{field.name}}','{{field.api}}')">{{key}}

                    {% else %}
                    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1"
                               onclick="check_radio(this,'{{ dict_to_str(value) }}','{{field.name}}','{{field.api}}')">{{key}}

                    {% endif %}


            {% endfor %}
            {% endif %}
            </td>
        </tr>
        {% endfor %}
    </table>

</div>

<script>
    function check(vi, fieldname, name, api) {
//        if (vi.checked) {
//   vi.parentNode.parentNode.style.backgroundColor="yellow";
//    alert(abc)
//            $.get(Flask.url_for("for_test"));
//            $.post(Flask.url_for("for_test"));

//        }
//        else {

//        }
//        alert("aaa");
        $.get(Flask.url_for("update_check", {name: name, api: api, fieldname: fieldname, ischecked: vi.checked}));
    }

    function check_radio(vi, fieldname_value, name, api) {

        $.get(Flask.url_for("update_radio", {
            name: name,
            api: api,
            fieldname_value: fieldname_value,
            ischecked: vi.checked
        }));
    }
</script>
</body>
</html>